<?PHP

class user
{
	var $user_id;
	
	var $user_password;
	var $user_email;
	var $user_name;
	var $user_last_login;
	var $user_level;
	var $user_serial;
	var $user_db;
	var $user_openid;
	var $user_where;
	var $user_count;
	var $user_order;
	var $user_files;
	var $user_data;
	var $user_error;
	var $user_mail_message;
	var $user_mail_subject;
	var $user_password_code;
	var $user_confirm_code;
	var $code;
	var $mode;
	var $data;
	var $post;
	var $ud;
	var $hash;
	
	
	/*
		Confirm COdes
		1 : OK
		0 : NOT Confirmed
		2 : Changed Password
	*/
function user_mailer()
{
	
	//setup initial variables
	// $message='\n';
	// $message.='\n';
	$mailer =& new emailer;
	global $conf;
	
	
	if($this->mode=='add')
	{
		$activate_url=$conf->absolute_path."/user/activate/".$this->code;

		$message='Welcome to '.$conf->site_name." <br />";
	 	$message.='Thank You and Enjoy for Registering, you can activate your account here:'."<br />"." <br />";
		$message.='-----------------------------------------------------------------------'." <br />";
		$message.=$activate_url." <br />"." <br />";
		$message.='Thank You and Enjoy'." <br />";
		
		$mailer->mail_message=$message;
		$mailer->mail_subject='Thank You and Enjoy for Signing up with '.$conf->site_name;
		$mailer->mail_to=$this->post['user_email'];
		$mailer->send_mail('quick');
		
	}
	else if($this->mode=='addactivate')
	{
		$message='Welcome to '.$conf->site_name." <br />";
	 	$message='Your Account has Been Activated, you may now login to our site'."<br />"." <br />";
		$message.='-----------------------------------------------------------------------'." <br />";
		$message.='Thank You and Enjoy'." <br />";
		
	//	echo $message;
		
		$mailer->mail_message=$message;
		$mailer->mail_subject=$conf->site_name.': Your Account Has Been Activated';
		$mailer->mail_to=$this->data['user_email'];
		$mailer->send_mail('quick');
		
	}else if($this->mode=='activate')
	{

	 	$message='Your Account has Been Activated, you may now login to our site'."<br />"." <br />";
		$message.='-----------------------------------------------------------------------'." <br />";
		$message.='Thank You and Enjoy'." <br />";
		
		echo $message;
		
		$mailer->mail_message=$message;
		$mailer->mail_subject=$conf->site_name.': Your Account Has Been Activated';
		$mailer->mail_to=$this->data['user_email'];
		$mailer->send_mail('quick');
		
	}
	else if($this->mode=='password')
	{
		$password_url=$conf->absolute_path."user/password/".$this->code;

		$message='Welcome to '.$conf->site_name." <br />";
	 	$message.='Click here to change your password:'."<br />"." <br />";
		$message.='-----------------------------------------------------------------------'." <br />";
		$message.=$password_url." <br />"." <br />";
		$message.='Thank You and Enjoy'." <br />";
		
		$mailer->mail_message=$message;
		$mailer->mail_subject='Password Has Been Changed ';
		$mailer->mail_to=$this->post['user_email'];
		$mailer->send_mail('quick');
		
	}
}
	function get_user_info()
	{
	//	echo 'gui';
		$where[0][0]='user_id';
		$where[0][1]=$this->user_id;
		$where[0][2]='=';
		$user_data=get_sql('user','user_id',$where,NULL);
		$user_data=current($user_data);
	
		$serial=unserialize($user_data['user_serial']);
		//	print_r($serial);
		$user_data=array_merge($user_data,$serial);
		$user_data['user_serial']='';
		$user_data[5]='';

		return $user_data;
	}
	
	function update_user()
	{
		$this->post=process_serialized_values($this->post);
		process_sql_post($this->post,'user','update','user_id',$this->user_id);
		mp('Successfully Modified');
	}
	function activate_user()
	{
		
		$table='user';
		$key='user_id';
	
		$w[0]='user_confirm_code';
		$w[1]=$this->user_confirm_code;
		$w[2]='=';	
		$where[]=$w;
		$data=get_sql($table,$key,$where,$o);
		$this->data=current($data);
		
		
		if(!empty($data))
		{
			mp("Confirmation Code Found, Activating Erase Code and Modify");
			$post['user_confirm']=1;
			$post['user_confirm_code']=0;
			process_sql_post($post,$table,'update','user_confirm_code',$this->user_confirm_code);
			return true;
		}
		else
		{
			mp("Confirmation Code NOT Found");
				return false;
		}	
				$this->mode='activate';
		
				$this->user_mailer();
	
		
	}

	function check_email_user()
	{
		$this->user_where[0][0]='user_name';
		$this->user_where[0][1]=$this->user_db['user_name'];
		$this->user_where[0][2]='=';
		$this->user_data=getlistfiltered2('user',NULL,NULL,$this->user_where,FALSE,FALSE,FALSE);

		if(!empty($this->user_data))
		{
			$this->user_error='USEREXISTS';
			//echo 'err';
			return false;
		}
		
		$this->user_where[0][0]='user_email';
		$this->user_where[0][1]=$this->user_db['user_email'];
		$this->user_where[0][2]='=';
		$this->user_data=getlistfiltered2('user',NULL,NULL,$this->user_where,FALSE,FALSE,FALSE);
		if(!empty($this->user_data))
		{
			$this->user_error='EMAILEXISTS';
			
			return false;
		}
		
		
		return true;
	}
	
	function add_user()
	{

		mp("Adding");
		$code=randomchar();
		$code=strtolower($code);
		mp("Processing Confirmation Code: ".$code);
		
		$this->post['user_confirm_code']=$code;
		if(empty($this->post['user_level']))
			{
				
					$this->post['user_level']=1;
			}
		process_sql_post($this->post,'user','add',NULL,NULL);
		$this->mode='add';
		$this->code=$code;
		$this->user_mailer();
		return $code;
	}
	
	function add_user_confirmed()
	{

		mp("Adding");
		$code=randomchar();
		$code=strtolower($code);
		mp("Processing Confirmation Code: ".$code);
		$this->post['user_confirm']=1;
		$this->post['user_confirm_code']=$code;
		$this->post['user_aswer']=strtoupper($this->post['user_aswer']);
		$this->post['user_name']=randomchar();
		$this->post['user_password']=md5($this->post['user_password']); 
		$this->post['user_email']=$_SESSION['user_data']['user_email'];
		
		
			foreach($this->post as $post_key => $value)
			{

				$haystack = $post_key;
				$needle = '_';
				$result = substr($haystack, 0, strpos($haystack, $needle));	

				if($result=='serial')
				{
				$y[$post_key]=$this->post[$post_key];	
				}

			}
			$this->post['user_serial']=serialize($y);
		if(empty($this->post['user_level']))
			{
				
					$this->post['user_level']=1;
			}
		process_sql_post($this->post,'user','add',NULL,NULL);
		$this->mode='addactivate';
		$this->code=$code;
		$this->user_mailer();
		return $this->post['user_email'];
	}

	function user_add()
	{
		
		foreach($this->user_db as $post_key => $value)
		{

			$haystack = $post_key;
			$needle = '_';
			$result = substr($haystack, 0, strpos($haystack, $needle));	
		
			if($result=='serial')
			{
			$y[$post_key]=$this->user_db[$post_key];	
			}
			
		}	
		
		
		if($this->user_db['user_confirm']==0)
		{
			$code=randomchar();
			$code=strtolower($code);
			$rows[]="user_confirm_code"; 
			$values[]=$code; 
			$this->user_mail_subject='Welcome';
			$this->user_email=$this->user_db['user_email'];
			$this->user_mail_message="Confirmation ID:".$code." user: ".$this->user_name;
			$this->user_mailer();
		}
		
		
		$rows[]="user_name"; 
		$rows[]="user_confirm"; 
		$rows[]="user_password"; 
		$rows[]="user_email"; 
		$rows[]="user_level"; 
		$rows[]="user_serial"; 
		$values[]=$this->user_db['user_name']; 
		$values[]=$this->user_db['user_confirm']; 
		$values[]=md5($this->user_db['user_password']); 
		$values[]=$this->user_db['user_email']; 
		$values[]=$this->user_db['user_level'];
		$values[]=serialize($y); 
		
			insertsqlv2("user",$rows,$values);
			$this->user_error='SUCCESS';
			return true;
			

	}
	function user_list()
	{
		if(!empty($this->user_where))
		{
			$where=$this->user_where;
		}
		else
		{
			$where=FALSE;
		}
		$this->user_data=getlistfiltered2('user',NULL,NULL,$where,FALSE,FALSE,FALSE);
	}
	
	
	
	function user_update()
	{
		foreach($this->user_db as $post_key => $value)
		{

			$haystack = $post_key;
			$needle = '_';
			$result = substr($haystack, 0, strpos($haystack, $needle));	
		
			if($result=='serial')
			{
			$y[$post_key]=$this->user_db[$post_key];	
			}
			
		}
		
		$rows[]="user_name"; 

		$rows[]="user_email"; 
	//	$rows[]="user_password"; 
		$rows[]="user_level"; 
		$rows[]="user_serial"; 
		$values[]=$this->user_db['user_name']; 
		$values[]=$this->user_db['user_email']; 
	//	$values[]=md5($this->user_db['user_password']); 
		$values[]=$this->user_db['user_level'];
		$values[]=serialize($y);
	//	////rp($y);
		updatesql2("user",$rows,$values,"user_id",$this->user_id);
		return "Updated!";
		
		
	}
	function get_user_data()
	{
		$user[0][0]='user_id';
		$user[0][1]=$this->user_id;
		$user[0][2]='=';
		$this->user_data=getlistfiltered2('user',NULL,NULL,$user,FALSE,FALSE,FALSE);
	}
	function user_delete()
	{
		
	}
	


	function change_password()
	{
		mp("Getting User Info");
		$where[0][0]='user_email';
		$where[0][1]=$this->post['user_email'];
		$where[0][2]='=';
		$user_data=get_sql('user','user_id',$where,NULL);
		$user_data=current($user_data);
		
		if(empty($user_data))
		{
					
			return false;
		}
		else
		{
			$code=randomchar();
			$this->post['user_password_code']=$code;
			process_sql_post($this->post,'user','update','user_id',$user_data['user_id']);
			
			$this->mode='password';
			$this->code=$code;
			$this->user_mailer();
			
			return true;
	
		}

				
	}
	
	function validate_password_code()
	{
		
		$where[0][0]='user_password_code';
		$where[0][1]=$this->user_password_code;
		$where[0][2]='=';
		$user_data=get_sql('user','user_id',$where,NULL);
		$this->ud=current($user_data);
		
		if(empty($this->ud))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	
	
	function update_password()
	{
		
		$where[0][0]='user_password_code';
		$where[0][1]=$this->post['user_password_code'];
		$where[0][2]='=';
		$user_data=get_sql('user','user_id',$where,NULL);
		$ud=current($user_data);
		
		if(empty($ud))
		{
					
			return false;
		}
		else
		{
						mp('Updating User Password to'.$this->post['user_password']);
			$this->post['user_password']=$this->post['user_password'];
			$this->post['user_serial']=$ud['user_serial'];
			$this->post['user_password_code']='';

			process_sql_post($this->post,'user','update','user_id',$ud['user_id']);
		/*
			TODO password changed mail
		*/	
		//	$this->mode='password';
		//	$this->code=$code;
		//	$this->user_mailer();
			
			return true;
	
		}
		
	}
	
	function usermailer()
	{
		$mailer =& new emailer;
		global $conf;

		$mailer->mail_name=$conf->site_name;
		$mailer->mail_to=$this->user_email;
		$mailer->mail_sender=$conf->site_name;
		$mailer->mail_from="no-reply@".$conf->site_url;
		$mailer->mail_message=$this->user_mail_message;
		$mailer->mail_subject=$this->user_mail_subject;
		$mailer->mail_reply_to="no-reply@".$conf->site_url;
		$answer=$mailer->sendemail();
	}
}
?>